<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :model="user"
      ref="loginForm"
      :rules="loginFormRules"
    >
      <el-form-item class="login-logo-box">
        <el-image style="width: 100px; height: 100px" :src="logoSrc"></el-image>
      </el-form-item>
      <el-form-item prop="username">
        <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input placeholder="请输入密码" v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree"
          >我已阅读并同意用户协议和隐私条款</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          @click="onLogin('loginForm')"
          :loading="loginBtnLoading"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/apis/user";

export default {
  name: "LoginIndex",
  data() {
    return {
      user: {
        username: "",
        password: "",
        agree: false,
      },
      checked: false, //登录时是否同意协议的选中状态
      loginBtnLoading: false,
      logoSrc: require("../../assets/logo.png"), //require引用图片
      // 登录表单校验
      loginFormRules: {
        username: [
          { required: true, message: "用户名必填", trigger: "blur" },
          { min: 4, max: 8, message: "长度在 4 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码必填", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        agree: [
          {
            // 自定义校验
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请勾选用户协议"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    // 登录
    onLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loginBtnLoading = true;
          const u = this.user;
          login(u)
            .then((res) => {
            //   console.log(res);
              const data = res.data;
              if (
                data.username === u.username &&
                u.password === "123456" &&
                res.status === 200
              ) {
                this.$message({
                  message: "您已登录",
                  type: "success",
                });
                // 登陆成功存储用户信息与token到本地
                window.localStorage.setItem("user", JSON.stringify(data));
                // 登陆成功跳转到首页
                this.$router.push("/");
              } else {
                this.$message.error("登陆失败");
              }
              this.loginBtnLoading = false;
            })
            .catch((err) => {
            //   console.log(err);
              this.loginBtnLoading = false;
            });
        } else {
        //   console.log("login validate fail!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: deepskyblue;
  .login-form {
    background-color: #fff;
    padding: 50px;
    min-width: 300px;
  }
  .login-btn {
    width: 100%;
  }
  .login-logo-box {
    display: flex;
    justify-content: center;
  }
}
</style>
